# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=harfbuzz
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         $([[ ${CARCH} == i686 ]] || echo \
           "${MINGW_PACKAGE_PREFIX}-${_realname}-cairo" \
           "${MINGW_PACKAGE_PREFIX}-${_realname}-icu" \
           "${MINGW_PACKAGE_PREFIX}-${_realname}-utils" \
           "${MINGW_PACKAGE_PREFIX}-${_realname}-docs"))
pkgver=11.2.1
pkgrel=1
pkgdesc="OpenType text shaping engine"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://harfbuzz.github.io/'
msys2_repository_url='https://github.com/harfbuzz/harfbuzz/'
msys2_references=(
  "archlinux: harfbuzz"
  "cpe: cpe:/a:harfbuzz_project:harfbuzz"
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-graphite2")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             $([[ ${CARCH} == i686 ]] || echo \
               "${MINGW_PACKAGE_PREFIX}-gtk-doc" \
               "${MINGW_PACKAGE_PREFIX}-icu" \
               "${MINGW_PACKAGE_PREFIX}-cairo" \
               "${MINGW_PACKAGE_PREFIX}-chafa")
             "${MINGW_PACKAGE_PREFIX}-ragel")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-fonttools"
              "${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=("https://github.com/harfbuzz/harfbuzz/releases/download/${pkgver}/harfbuzz-${pkgver}.tar.xz"
        "001-fix-build-with-chafa.patch"
        "002-make-library-type-in-cmake-config-unknown.patch")
sha256sums=('093714c8548a285094685f0bdc999e202d666b59eeb3df2ff921ab68b8336a49'
            '26b37a1ca9872973905ecb96bcbe3f054472252320956faa74428206900d360e'
            '6ce1063f2ac77db0b4705153031e2c6ba8d7a65a04eb759fe4178e9445406ddb')
noextract=("harfbuzz-${pkgver}.tar.xz")

prepare() {
  echo "Extracting harfbuzz-${pkgver}.tar.xz..."
  tar -xJf harfbuzz-${pkgver}.tar.xz || true

  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-fix-build-with-chafa.patch
  patch -p1 -i "${srcdir}"/002-make-library-type-in-cmake-config-unknown.patch
}

build() {
  local -a _static_flags=(
    -DGIO_STATIC_COMPILATION
    -DGLIB_STATIC_COMPILATION
    -DGMODULE_STATIC_COMPILATION
    -DGOBJECT_STATIC_COMPILATION
    -DGRAPHITE2_STATIC
    -DCAIRO_WIN32_STATIC_BUILD
  )

  declare -a _extra_config
  if [[ ${CARCH} != i686 ]]; then
    _extra_config+=(
      "-Dchafa=enabled"
      "-Dcairo=enabled"
      "-Dicu=enabled"
      "-Ddocs=enabled"
      "-Dutilities=enabled"
    )
  else
    _extra_config+=(
      "-Dchafa=disabled"
      "-Dcairo=disabled"
      "-Dicu=disabled"
      "-Ddocs=disabled"
      "-Dutilities=disabled"
    )
  fi

  CFLAGS+=" ${_static_flags[@]}" \
  CXXFLAGS+=" ${_static_flags[@]}" \
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson setup \
    --prefix="${MINGW_PREFIX}" \
    --buildtype plain \
    --wrap-mode=nofallback \
    --default-library=static \
    -Dauto_features=enabled \
    -Dintrospection=disabled \
    -Dgobject=enabled \
    -Dicu=enabled \
    -Dgdi=enabled \
    -Dgraphite=enabled \
    -Ddirectwrite=enabled \
    -Dtests=disabled \
    -Ddocs=disabled \
    "${_extra_config[@]}" \
    "build-${MSYSTEM}-static" \
    "${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/meson compile -C "build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson setup \
    --prefix="${MINGW_PREFIX}" \
    --buildtype plain \
    --wrap-mode=nofallback \
    --default-library=shared \
    -Dauto_features=enabled \
    -Dintrospection=enabled \
    -Dgobject=enabled \
    -Dgdi=enabled \
    -Dgraphite2=enabled \
    -Ddirectwrite=enabled \
    -Dtests=disabled \
    "${_extra_config[@]}" \
    "build-${MSYSTEM}-shared" \
    "${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/meson compile -C "build-${MSYSTEM}-shared"
}

check(){
  cd "${srcdir}/build-${MSYSTEM}-shared"
  ${MINGW_PREFIX}/bin/meson setup -Dtests=enabled ../${_realname}-${pkgver}
  ${MINGW_PREFIX}/bin/meson compile
  ${MINGW_PREFIX}/bin/meson test
}

package_harfbuzz() {
  pkgdesc+=" (mingw-w64)"

  ${MINGW_PREFIX}/bin/meson install -C "build-${MSYSTEM}-static" --destdir "${pkgdir}"

  ${MINGW_PREFIX}/bin/meson install -C "build-${MSYSTEM}-shared" --destdir "${pkgdir}"

  if [[ ${CARCH} != i686 ]]; then

  mkdir -p dest-docs${MINGW_PREFIX}/share
  mv "${pkgdir}"${MINGW_PREFIX}/share/gtk-doc dest-docs${MINGW_PREFIX}/share/gtk-doc

  mkdir -p dest-utils${MINGW_PREFIX}/bin
  mv "${pkgdir}"${MINGW_PREFIX}/bin/*.exe dest-utils${MINGW_PREFIX}/bin/

  mkdir -p dest-cairo${MINGW_PREFIX}/bin
  mkdir -p dest-cairo${MINGW_PREFIX}/include/harfbuzz/
  mkdir -p dest-cairo${MINGW_PREFIX}/lib/pkgconfig
  mv "${pkgdir}"${MINGW_PREFIX}/bin/libharfbuzz-cairo*.dll dest-cairo${MINGW_PREFIX}/bin/
  mv "${pkgdir}"${MINGW_PREFIX}/include/harfbuzz/hb-cairo.h dest-cairo${MINGW_PREFIX}/include/harfbuzz/
  mv "${pkgdir}"${MINGW_PREFIX}/lib/libharfbuzz-cairo*.a dest-cairo${MINGW_PREFIX}/lib/
  mv "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/harfbuzz-cairo.pc dest-cairo${MINGW_PREFIX}/lib/pkgconfig/

  mkdir -p dest-icu${MINGW_PREFIX}/bin
  mkdir -p dest-icu${MINGW_PREFIX}/include/harfbuzz/
  mkdir -p dest-icu${MINGW_PREFIX}/lib/pkgconfig
  mv "${pkgdir}"${MINGW_PREFIX}/bin/libharfbuzz-icu*.dll dest-icu${MINGW_PREFIX}/bin/
  mv "${pkgdir}"${MINGW_PREFIX}/include/harfbuzz/hb-icu.h dest-icu${MINGW_PREFIX}/include/harfbuzz/
  mv "${pkgdir}"${MINGW_PREFIX}/lib/libharfbuzz-icu*.a dest-icu${MINGW_PREFIX}/lib/
  mv "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/harfbuzz-icu.pc dest-icu${MINGW_PREFIX}/lib/pkgconfig/

  fi

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}

package_harfbuzz-cairo() {
  pkgdesc+=" - Cairo integration (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-harfbuzz"
           "${MINGW_PACKAGE_PREFIX}-cairo")

  mv dest-cairo/* "${pkgdir}"
}

package_harfbuzz-icu() {
  pkgdesc+=" - ICU integration (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-harfbuzz"
           "${MINGW_PACKAGE_PREFIX}-icu")

  mv dest-icu/* "${pkgdir}"
}

package_harfbuzz-utils() {
  pkgdesc+=" - Utilities (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-harfbuzz"
           "${MINGW_PACKAGE_PREFIX}-harfbuzz-cairo"
           "${MINGW_PACKAGE_PREFIX}-cairo"
           $([[ ${CARCH} == i686 ]] || echo "${MINGW_PACKAGE_PREFIX}-chafa"))

  mv dest-utils/* "${pkgdir}"
}

package_harfbuzz-docs() {
  pkgdesc+=" - Documentation (mingw-w64)"
  depends=()
  optdepends=()

  mv dest-docs/* "${pkgdir}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
